<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img alt="" class="logo_img" src="@/assets/logo.png">
      </div>

      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login_form" label-width="0px">


        <!--输入账号的输入框-->
        <el-form-item prop="name">
          <el-input v-model="loginForm.name" placeholder="请输入账号"></el-input>
        </el-form-item>
        <!--输入密码的输入框-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <!--显示按钮的区域-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>


      </el-form>
    </div>
  </div>
</template>

<script>


export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        name: '',
        password: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login() {
      let flag = true;
      this.$refs.loginForm.validate(valid => {
        if (!valid) {
          flag = false;
        }
      })
      if (!flag) {
        return;
      }
      this.$http.post("/api/user/login", this.loginForm)
          .then(res => {
            localStorage.setItem("token", res.data)
            this.$router.push("/home")
          }).catch(res => {
      })
    },
    register() {
      this.$router.push("/register")
    },
    reset() {
      this.loginForm = {}
    }
  }

}
</script>

<style scoped>

.login_container {
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #2b4b6b;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.logo_img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #2b4b6b;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {

  text-align: center;

}
</style>